﻿//#define Log
using UnityEngine;
using System.Collections;
using AssetBundleHelper;
using HotUI;

/// <summary>
/// Game tool.
/// </summary>
public class GameTool{

	public static void Log(string nn){
		Debug.Log("GTOOL"+nn);
	}
	#region AB
	public static void PrestrainLoad(PrestrainData data){
		if(data.assetDataType == ePrestrainDataType.GameObject){
			AssetBundleDataMgr.Instance.data.getABObject(data.name,data.path);
		}
	}
	public static Object ResourcesLua(string name){
		var path = "LuaTxt/"+name;
		#if Resources && UNITY_EDITOR
			var obj = Resources.Load(path);
			if(obj == null){
				Debug.Log(string.Format("Load Lua.txt[{0}] is null",path));
			}
			return obj;
		#else
			return LoadObject(name,path);
		#endif
	}
	public static Object LoadObject(string name,string defaultPath = "Data/"){
		var abobj = AssetBundleDataMgr.Instance.data.getABObject (name,defaultPath);
		if (abobj == null) {
			#if Log
			Debug.Log(string.Format("没找到对象 准备使用ResourLoad:{0}{1}",defaultPath,name));
			#endif
			abobj = ABObject.CreateObject (name, defaultPath);
			AssetBundleDataMgr.Instance.data.AddABObject (name, abobj);
			if (abobj.asset == null) {
				#if Log
				Debug.Log(string.Format("！！！Resources  还是没有找到对象 :{0}{1}",defaultPath,name));
				#endif
			}
		}
		return abobj.asset;
	}
	public static void LoadMainAB(System.Action callback){
		AssetBundleDataMgr.Instance.data.getAssetBundleMainfest (callback);
	}
	public static bool ABMainLoadFinish{
		get{
			return AssetBundleDataMgr.Instance.data.assetbundleManifest != null;
		}
	}

	public static void LoadObject(string name,Transform parent,System.Action<ABObject> callback = null,string path = "",float despawn = 0){
		AssetBundleDataMgr.Instance.data.getABObject (name, delegate(ABObject ab) {
			if(ab == null){
				#if Log
				Debug.Log(string.Format("使用Resrources.Load[{0}]",name));
				#endif
				if(path == "")
					path = "Prefab/";
				ab = ABObject.CreateGameObject(name,path);
				AssetBundleDataMgr.Instance.data.AddABObject (name,ab);
			}
			ab.AddChild(parent,ObjManager.Instance.ParentDefualt);
			if(callback != null)
				callback(ab);
		},name,path);	
	}
	public static void LoadUIObject(string name,Transform parent,System.Action<ABObject> callback = null,string path = "",float despawn = 0){
		AssetBundleDataMgr.Instance.data.getABObject (name, delegate(ABObject ab) {
			if(ab == null){
				if(path == "")
					path = "ViewUI/";
				ab = ABObject.CreateGameObject(name,path);
				AssetBundleDataMgr.Instance.data.AddABObject (name,ab);
			}
			ab.AddChild(parent,ObjManager.Instance.ParentUI);
			if(callback != null)
				callback(ab);
		},name,"ViewUI/");	
	}
	public static void LoadScene(string name,System.Action<ABObject> callback = null){
		#if UNITY_EDITOR && Resources
			UnityEngine.SceneManagement.SceneManager.LoadScene(name);
		#else
			var ab = AssetBundleDataMgr.Instance.data.getSceneAB (name);
			if(ab == null){
				UnityEngine.SceneManagement.SceneManager.LoadScene(name);
			}else{
				ab.LoadScene();
				if(callback != null)
					callback(ab);
			}
		#endif
	}

	public static void LoadJSON(string name,System.Action<ABObject> callback = null){
		AssetBundleDataMgr.Instance.data.getABObject (name, delegate(ABObject ab) {
			if(ab == null){
				ab = ABObject.CreateJSON(name);
				AssetBundleDataMgr.Instance.data.AddABObject (name,ab);
			}

			ab.AsJSONNode();
			if(callback != null)
				callback(ab);
		},name,"Data/");	
	}
	#endregion

	#region ui
	public static LuaView OpenStateUI(string uiname,object arg){
		return UIMgr.Instance.SetState(uiname,arg);
	}
	public static LuaView OpenDialog(string uiname,object arg)
	{
		return UIMgr.Instance.SetDialogState(uiname,arg);
	}
	public static void CloseDialog(){
		UIMgr.Instance.CloseDialog();
	}
	public static void CloseAllDialog(){
		UIMgr.Instance.CloseAllDialog();
	}
	public static void ShowFlashText(string str,float time){
		var dia = new DialogFlashText(str,time);
		var dialog = OpenDialog("dialog_flash_text",JsonUtility.ToJson(dia));
	}
	#endregion
}
[System.Serializable]
public struct DialogFlashText{
	public string info;
	public float time;
	public DialogFlashText(string _info,float _time){
		info =_info;
		time = _time;
	}
}
